package com.itheima.test3.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.QueryPageBean;
import com.itheima.bean.Result;
import com.itheima.test3.entity.Subject;
import com.itheima.test3.mapper.SubjectMapper;
import com.itheima.test4.entity.Answer;
import com.itheima.test4.mapper.AnswerMapper;
import com.itheima.utils.DateUtils;
import com.itheima.vo.question;
import com.itheima.vo.subjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 试题表 前端控制器
 * </p>
 *
 * @author itheima
 * @since 2021-03-28
 */
@RestController
@RequestMapping("/subject")
public class SubjectController {

    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private AnswerMapper answerMapper;

    /**
     * @description:分页
     * @author: Java
     * @date: 2021/3/28 13:14
     * @param: queryPageBean
     * @return: com.itheima.bean.Result
     */
    @PostMapping("findPage")
    public Result findPage(int id, @RequestBody QueryPageBean queryPageBean) {
        try {
            //1. 构建分页对象
            Page<Subject> page = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            //2.创建查询条件构建器
            QueryWrapper<Subject> wrapper = new QueryWrapper<>();
            //3.设置条件
            wrapper.eq("course_id", id);
            //4.调用业务查询
            IPage<Subject> pageResult = subjectMapper.selectPage(page, wrapper);
            //5.封装到Result返回
            return new Result(true, "查询学生成功", pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "失败");
        }

    }

    /**
     * @description:禁用题库
     * @author: Java
     * @date: 2021/3/28 11:09
     * @param: id
     * @return: com.itheima.bean.Result
     */
    @PostMapping("EnableStatus")
    public Result EnableStatus(int id) {
        try {
            Subject subject = new Subject();
            subject.setId(id);
            subject.setStatus(1);
            //2.直接更新
            subjectMapper.updateById(subject);
            //3.响应
            return new Result(true, "启用成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "启用失败");
        }
    }

    /**
     * @description:启用题库
     * @author: Java
     * @date: 2021/3/28 11:09
     * @param: id
     * @return: com.itheima.bean.Result
     */
    @PostMapping("DisableStatus")
    public Result DisableStatus(int id) {
        try {
            Subject subject = new Subject();
            subject.setId(id);
            subject.setStatus(0);
            //2.直接更新
            subjectMapper.updateById(subject);
            //3.响应
            return new Result(true, "禁用成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "禁用失败");
        }
    }

    /**
     * @description:删除试题
     * @author: Java
     * @date: 2021/3/28 11:21
     * @param: id
     * @return: com.itheima.bean.Result
     */
    @PostMapping("delete")
    public Result delete(int id) throws IOException {
        System.out.println("subjectMapper里delete执行...id=" + id);
        //不讲道理，直接删
        subjectMapper.deleteById(id);
        return new Result(true, "删除成功");
    }

    /**
     * @description:添加题库
     * @author: Java
     * @date: 2021/3/28 11:27
     * @param: id:用户id subject：要添加的对象
     * @return: com.itheima.bean.Result
     */
    @PostMapping("add")
    public Result add(int id, @RequestBody Subject subject) {
        try {
            // 1.创建日期
            subject.setCreateDate(DateUtils.parseDate2String(new Date()));
            // 2.设置权限，0是未启用
            subject.setStatus(0);
            // 3.设置类型，1是单选
            subject.setType(1);
            // 4.默认为"空"
            if (subject.getSubject() == null) subject.setSubject("空");
            // 5.添加题库id
            subject.setCourseId(id);
            subjectMapper.insert(subject);
            // 6.响应
            return new Result(true, "新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增失败");
        }


    }

    /**
     * @description:获取题目
     * @author: Java
     * @date: 2021/4/5 0:48
     * @param: id
     * @return: com.itheima.bean.Result
     */
    @PostMapping("getSubject")
    public Result getSubject(@RequestBody Answer temp) {
        try {
            Integer uid = temp.getUserId();
            Integer cid = temp.getCourseId();
            System.out.println("uid = " + uid);
            System.out.println("cid = " + cid);
            // 0new一个subjectVo
            subjectVo subjectVo = new subjectVo();
            // 0.1设置用户id
            subjectVo.setUserId(uid);

            // 1拿题目
            // 1.1创建查询条件构建器
            QueryWrapper<Subject> wrapper = new QueryWrapper<>();
            // 1.2设置条件
            wrapper.eq("course_id", cid)
                    .eq("status", 1);
            // 1.3开查
            List<Subject> subjectList = subjectMapper.selectList(wrapper);
            // 1.4放选项
            ArrayList<String> choice = new ArrayList<>();
            choice.add("没有");
            choice.add("很轻");
            choice.add("中等");
            choice.add("偏重");
            choice.add("严重");
            for (Subject subject : subjectList) {
                subject.setChoice(choice);
            }
            // 1.5放入题目集合
            subjectVo.setQuestions(subjectList);

            // 2拿答案
            // 2.1创建查询条件构建器
            QueryWrapper<Answer> wrapper1 = new QueryWrapper<>();
            // 2.2设置条件
            wrapper1.eq("user_Id", uid)
                    .eq("course_id", cid);
            // 2.3开查
            Answer answer = answerMapper.selectOne(wrapper1);
            if (answer != null) {
                String answerString = answer.getAnswer();
                List<String> answerList = Arrays.asList(answerString.split(","));
                // 2.4放入选项集合
                subjectVo.setAnswer(answerList);
            }

            // 3拿题库名字
            String courseName = subjectMapper.getCourseName(cid);
            // 3.1放入名字
            subjectVo.setCoursename(courseName);


            // 4返回给页面
            return new Result(true, "查询题目成功", subjectVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "失败");
        }

    }

}
